package com.reggie.service.impl;


import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.reggie.mapper.EmployeeMapper;
import com.reggie.pojo.Employee;
import com.reggie.service.EmployeeService;
import com.reggie.util.Md5Util;
import com.reggie.util.PageUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class EmployeeServiceImpl implements EmployeeService {
    @Autowired
    EmployeeMapper employeeMapper;

    @Override
    public Employee login(String username, String password) {
        String Md5Password = Md5Util.getMD5String(password);
        Employee employee = employeeMapper.find(username, Md5Password);
        return employee;
    }

    @Override
    public PageUtil<Employee> getEmployeeList(Integer page, Integer pageSize, String name) {
        PageUtil<Employee> employeePageUtil = new PageUtil<>();
        PageHelper.startPage(page, pageSize);

        List<Employee> employeeList = employeeMapper.findAll(page, pageSize, name);
        if (employeeList == null) return null;

        Page<Employee> employeePage = (Page<Employee>) employeeList;
        employeePageUtil.setTotal(employeePage.getTotal());
        employeePageUtil.setRecords(employeePage.getResult());
        return employeePageUtil;
    }

    @Override
    public Employee getEmployeeById(Long id) {
        Employee employee = employeeMapper.findById(id);
        return employee;
    }

    @Override
    public boolean addEmployee(Employee employee) {
        employee.setPassword("e10adc3949ba59abbe56e057f20f883e");//"123456"的MD5加密形式
        return employeeMapper.add(employee) == 1;
    }

    @Override
    public boolean editEmployee(Employee employee) {
        employee.setPassword("e10adc3949ba59abbe56e057f20f883e");
        return employeeMapper.edit(employee) == 1;
    }
}
